package com.kbbywyclient.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.data.entity.Event;
import com.kbbywyclient.data.entity.ResultInfoCre;
import com.kbbywyclient.ui.adapter.base.BaseLvAdapter;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by FHW
 * on 2017/4/10
 */

public class CustomerTableAdapter extends BaseLvAdapter<ResultInfoCre> {


    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_education)
        TextView tv_education;
        @BindView(R.id.tv_occupation)
        TextView tv_occupation;
        @BindView(R.id.tv_credit)
        TextView tv_credit;
        @BindView(R.id.iv_buy)
        ImageView iv_buy;
        @BindView(R.id.tv_time)
        TextView tv_time;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public CustomerTableAdapter(Context context, int itemLayout, List<ResultInfoCre> list) {
        super(context, itemLayout, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ResultInfoCre resultInfo = getItem(position);

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = getItemView();
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ViewUtils.setText(holder.tv_name, resultInfo.getUserName());

        if (StringUtil.isNotEmpty(resultInfo.getHighestEducation())) {
            holder.tv_education.setVisibility(View.VISIBLE);
            ViewUtils.setText(holder.tv_education, resultInfo.getHighestEducation());
        }
        if (StringUtil.isNotEmpty(resultInfo.getUnitNature())) {
            holder.tv_occupation.setVisibility(View.VISIBLE);
            ViewUtils.setText(holder.tv_occupation, resultInfo.getUnitNature());
        }
        if (StringUtil.isNotEmpty(resultInfo.getCreditCard())) {
            holder.tv_credit.setVisibility(View.VISIBLE);
            ViewUtils.setText(holder.tv_credit, resultInfo.getCreditCard());
        }

        ViewUtils.setText(holder.tv_time, resultInfo.getApplyDateTime());

        holder.iv_buy.setOnClickListener(v -> {
            EventBus.getDefault().post(new Event.CustomerTableBuy(position));
        });

        return convertView;
    }

}
